﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using XYTXL.AppCode;
using XYTXL.Data;
using XYTXL.API;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace XYTXL.Web.View.UCMangForm
{
    public partial class STGL : FormUControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 重写父类的保存数据方法
        /// </summary>
        /// <param name="intFormID">数据ID</param>
        /// <param name="strActionType">操作类型("ADD":"添加","UPDATE":"修改")</param>
        /// <returns></returns>
        public override bool SaveForm(int intFormID, string strActionType, int pid)
        {
            //执行 父类的方法
            base.SaveForm(intFormID, strActionType);

            //保存数据
            JH_WJ_STGL Model = new JH_WJ_STGLB().GetEntity(d => d.ID == intFormID);
            if (!string.IsNullOrEmpty(txtName.Text))
            {
                Model.WJID = Int32.Parse(txtName.Text);
            }
            Model.Details = txtDetails.Text;
            Model.Type = ddlType.SelectedValue;
            if (ddlType.SelectedValue != "论述题")
            {
                //var list1 = new JH_WJ_XXGLB().GetEntities(" STID='" + Model.ID + "' ");
                //foreach (var l in list1)
                //{
                //    new JH_WJ_XXGLB().Delete(l);
                //}

                string str = json.Value;
                List<JObject> list = JsonConvert.DeserializeObject<List<JObject>>(str); ;

                foreach (var obj in list)
                {
                    var xx1 = new JH_WJ_XXGLB().GetEntities(" STID='" + Model.ID + "' and Type='" + obj["Type"].ToString() + "' ").FirstOrDefault();

                    if (xx1 != null)
                    {
                        if (xx1.Name != obj["Name"].ToString())
                        {
                            xx1.Name = obj["Name"].ToString();
                            new JH_WJ_XXGLB().Update(xx1);
                        }
                    }
                    else
                    {
                        JH_WJ_XXGL xx = new JH_WJ_XXGL();
                        xx.STID = Model.ID;
                        xx.Type = obj["Type"].ToString();
                        xx.Name = obj["Name"].ToString();
                        xx.CRUser = Model.CRUser;
                        xx.CRDate = DateTime.Now;

                        new JH_WJ_XXGLB().Insert(xx);
                    }
                }
            }
            return new JH_WJ_STGLB().Update(Model);
        }




        public override void initMangerForm(int intFormID, string strActionType)
        {
            base.initMangerForm(intFormID, strActionType);

            JH_WJ_STGL Model = new JH_WJ_STGLB().GetEntity(d => d.ID == intFormID);
            txtName.Text = Model.WJID.ToString();
            txtDetails.Text = Model.Details;
            ddlType.SelectedValue = Model.Type;
            if (Model.Type != "论述题")
            {
                var list = new JH_WJ_XXGLB().GetEntities(" STID='"+Model.ID+"' ");
                json.Value = JsonConvert.SerializeObject(list);
            }
        }
    }
}